package com.pb.b2b.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pb.b2b.common.exception.CustomException;
import com.pb.b2b.common.service.impl.BaseServiceImpl;
import com.pb.b2b.dao.CategoryDAO;
import com.pb.b2b.dao.LanguageDAO;
import com.pb.b2b.model.Category;
import com.pb.b2b.model.CategoryLanguage;
import com.pb.b2b.model.Language;
import com.pb.b2b.model.enums.Status;
import com.pb.b2b.service.CategoryService;

@Service
public class CategoryServiceImpl extends BaseServiceImpl implements CategoryService {
	@Autowired
	private CategoryDAO categoryDAO;

	@Autowired
	private LanguageDAO languageDAO;

	@Transactional
	public void createCategory(Category category) {
		try {
			category.setStatus(Status.AKTIVE);
			category.setDateAdded(new Date());
			category.setDateModified(new Date());

			// pasif dil var ise verleri default ile ayni set ediliyor
			List<Language> disableLanguages = languageDAO.getLanguages(Status.PASSIVE);
			if (disableLanguages != null && disableLanguages.size() > 0) {
				Language defaultLanguage = languageDAO.getDefaultLanguage();
				CategoryLanguage defaultCategoryLanguage = new CategoryLanguage();
				for (CategoryLanguage categoryLanguage : category.getCategoryLanguages()) {
					if (categoryLanguage.getLanguage().getId().equals(defaultLanguage.getId())) {
						defaultCategoryLanguage = categoryLanguage;
						break;
					}
				}

				List<CategoryLanguage> disableCategoryLanguages = new ArrayList<CategoryLanguage>();
				for (Language language : disableLanguages) {
					CategoryLanguage categoryLanguage = new CategoryLanguage();
					categoryLanguage.setLanguage(language);
					categoryLanguage.setCategory(category);
					// pasif dillere de default dilin verileri set edilir.
					categoryLanguage.setDescription(defaultCategoryLanguage.getDescription());
					categoryLanguage.setKeywords(defaultCategoryLanguage.getKeywords());
					categoryLanguage.setName(defaultCategoryLanguage.getName());

					disableCategoryLanguages.add(categoryLanguage);
				}

				category.getCategoryLanguages().addAll(disableCategoryLanguages);
			}

			save(category);

		} catch (Exception e) {
			new CustomException(e, CategoryServiceImpl.class);
		}
	}

	@Transactional
	public void categoryUpdate(Category category) {
		try {
			category.setDateModified(new Date());
			merge(category);
		} catch (Exception e) {
			new CustomException(e, CategoryServiceImpl.class);
		}
	}

	// @Transactional(propagation=Propagation.REQUIRED)
	// public void registerStats(ImageCreativesStat imageCreativeStat){
	// List lst = getIcsByDimainAndCreative(imageCreativeStat);
	// if(lst.size() == 1){
	// imageCreativeStat.setId(lst.get(0).getId());
	// imageCreativeStat.setImp_delivered(lst.get(0).getImp_delivered() +
	// imageCreativeStat.getImp_delivered());
	// imageCreativeStat.setClick_delivered(lst.get(0).getClick_delivered() +
	// imageCreativeStat.getClick_delivered());
	// }
	// getHibernateTemplate().saveOrUpdate(imageCreativeStat);
	// }
	//
	// @Transactional(propagation=Propagation.REQUIRED)
	// public void registerStats(ImageCreativesStat imageCreativeStat){
	// ImageCreativesStat ics;
	// List lst = getIcsByDomainAndCreative(imageCreativeStat);
	// if(lst.size() == 1){
	// ics = lst.get(0);
	// ics.setImp_delivered(ics.getImp_delivered() +
	// imageCreativeStat.getImp_delivered());
	// ics.setClick_delivered(ics.getClick_delivered() +
	// imageCreativeStat.getClick_delivered());
	// getHibernateTemplate().update(ics);
	// }else{
	// getHibernateTemplate().save(imageCreativeStat);
	// }
	// }

}
